import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/utils/log.dart';

//暂时留着，备用
class LocalLanguageDialogue extends StatelessWidget {
  final List locales = [
    {'name': 'ENGLISH', 'locale': Locale('en', 'US')},
    {'name': '中文', 'locale': Locale('zh', 'CN')},
    {'name': '繁体', 'locale': Locale('zh_Hant', 'TW')},
//    {'name': '韩语', 'locale': Locale('ko', '')},
  ];

  final UserController _userController = Get.find();

  updateLanguage(Locale locale) {
    Get.back();
    Get.updateLocale(locale);
  }

  buildLanguageDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (builder) {
          return AlertDialog(
            title: Text('Choose Language'.tr),
            content: Container(
              width: double.maxFinite,
              child: ListView.separated(
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: GestureDetector(
                        child: Text(locales[index]['name']),
                        onTap: () {
                          Log.debug(locales[index]['name']);
                          switch (locales[index]['name']) {
                            case 'ENGLISH':
                              _userController.profile.value.lang = 0;
                              break;
                            case '中文':
                              _userController.profile.value.lang = 1;
                              break;
                            case '繁体':
                              _userController.profile.value.lang = 2;
                              break;
                            // case '韩语':
                            //   _userController.profile.value.lang = 3;
                            // break;
                            default:
                              _userController.profile.value.lang = 0;
                          }
                          updateLanguage(locales[index]['locale']);
                        },
                      ),
                    );
                  },
                  separatorBuilder: (context, index) {
                    return Divider(
                      color: Colors.blue,
                    );
                  },
                  itemCount: locales.length),
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        onPressed: () {
          buildLanguageDialog(context);
        },
        child: Text('Change Language'.tr));
  }
}
